package org.rsbot.script.wrappers;

import org.rsbot.client.Client;
import org.rsbot.client.HardReference;
import org.rsbot.client.Node;
import org.rsbot.script.methods.MethodContext;
import org.rsbot.script.methods.MethodProvider;
import org.rsbot.script.methods.Nodes;

public class RSItem extends MethodProvider
{
  private int id;
  private int stack;
  private RSComponent component;

  public RSItem(MethodContext paramMethodContext, int paramInt1, int paramInt2)
  {
    super(paramMethodContext);
    this.id = paramInt1;
    this.stack = paramInt2;
  }

  public RSItem(MethodContext paramMethodContext, org.rsbot.client.RSItem paramRSItem) {
    super(paramMethodContext);
    this.id = paramRSItem.getID();
    this.stack = paramRSItem.getStackSize();
  }

  public RSItem(MethodContext paramMethodContext, RSComponent paramRSComponent) {
    super(paramMethodContext);
    this.id = paramRSComponent.getComponentID();
    this.stack = paramRSComponent.getComponentStackSize();
    this.component = paramRSComponent;
  }

  public RSItemDef getDefinition()
  {
    try
    {
      Node localNode = this.methods.nodes.lookup(this.methods.client.getRSItemDefLoader(), this.id);

      if (localNode != null) {
        if ((localNode instanceof HardReference))
          return new RSItemDef((org.rsbot.client.RSItemDef)(org.rsbot.client.RSItemDef)((HardReference)localNode).get());
        if ((localNode instanceof org.rsbot.client.SoftReference)) {
          Object localObject = ((org.rsbot.client.SoftReference)localNode).getReference().get();

          if (localObject != null)
            return new RSItemDef((org.rsbot.client.RSItemDef)localObject);
        }
      }
      return null; } catch (ClassCastException localClassCastException) {
    }
    return null;
  }

  public int getID()
  {
    return this.id;
  }

  public int getStackSize()
  {
    return this.stack;
  }

  public boolean hasDefinition()
  {
    return getDefinition() != null;
  }

  public RSComponent getComponent()
  {
    return this.component;
  }

  public boolean isComponentValid()
  {
    return (this.component != null) && (this.component.isValid());
  }

  public String getName()
  {
    if (this.component != null) {
      return this.component.getComponentName();
    }
    RSItemDef localRSItemDef = getDefinition();
    if (localRSItemDef != null) {
      return localRSItemDef.getName();
    }

    return null;
  }

  public boolean doAction(String paramString)
  {
    if (this.component != null) {
      return this.component.doAction(paramString);
    }
    return false;
  }

  public boolean doClick(boolean paramBoolean)
  {
    return (this.component != null) && (this.component.doClick(paramBoolean));
  }
}